/**
 * 实现文件上传
 * 需要jqueryui支持core.js支持
 */
 $(function(){
    window.upload =(function(){
        var dia_up = $("<div id='_upload' style='display:none' title='上传文件'><iframe name='_frm_upload'  src='about:blank' width='380' height='100' frameborder='0' scrolling='no'></iframe></div>");
        dia_up.appendTo("body");
        dia_up.dialog({
            height: 220,
            width:430,
            closeOnEscape: false,
            modal: true,
            autoOpen:false,
            buttons: {
                "确定":function(){
                    console.log(file);
                    console.log(data);
                    if(file.length > 0 && data.length > 0){
                        callback.call(this, file, data);
                        dia_up.dialog("close");
                    } else {
                        alert("你还没上传附件！");
                    }
                },
                "取消":function(){
                    dia_up.dialog("close");
                }
            },
            beforeClose: function(){
                file = [];
                data = [];
                window.frames["_frm_upload"].location.href="about:blank";
            }
        });
        
		var iframe = dia_up.find("iframe");
        var callback = function(){};
        var config = "";
        var file = [];
        var data = [];
        var unParseJSON = function (obj) {
            var t = typeof (obj);
            if (t != "object" || obj === null) {
                // simple data type
                if (t == "string") obj = '"'+obj+'"';
                return String(obj);
            }
            else {
                // recurse array or object
                var n, v, json = [], arr = (obj && obj.constructor == Array);
                for (n in obj) {
                    v = obj[n]; t = typeof(v);
                    if (t == "string") v = '"'+v+'"';
                    else if (t == "object" && v !== null) v = JSON.stringify(v);
                    json.push((arr ? "" : '"' + n + '":') + String(v));
                }
                return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
            }
        };
        return {
            open:function(){
				iframe.attr("src", "../uploads/index.aspx?config=" + config + "&_=" + new Date().getTime());
                dia_up.dialog("open");
            },
            close:function(){
                dia_up.dialog("close");
            },
            init:function(cf, cb){
				if(cf && cb){
					config = encodeURIComponent(unParseJSON(cf));
					callback = cb;
				} else {
					alert("方法：upload.init参数不完整，请检查！");
				}
            },
			callback: function(f, d){
			    console.log(file);
				file.push(f);
				data.push(d);
			}
        };
	})();
 });
